package service;

import java.io.Closeable;
import java.nio.channels.SocketChannel;

public interface IoProvider extends Closeable {
    void register(HandleProviderCallback callback) throws Exception;

    void unregister(SocketChannel channel);

    abstract class HandleProviderCallback implements Runnable {
        private final IoProvider ioProvider;
        protected HandleProviderCallback(IoProvider provider, SocketChannel channel, int ops) {
            this.ioProvider = provider;
        }

        @Override
        public final void run() {
        }

        public final boolean onProcessIo() {
            return true;
        }

        /**
         * 可以进行接收或者发送时的回调
         *
         * @param args 携带之前的附加值
         */
        protected abstract boolean onProvideIo(Object args);

        /**
         * 检查当前的附加值是否未null，如果处于自循环时当前附加值不为null，
         * 此时如果外层有调度注册异步发送或者接收是错误的
         */
        public void checkAttachNull() {
        }
    }
}